<!DOCTYPE html>


<html lang="zh-Hans">


<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="代码,生活,杂记,思考" />
   
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    正则表达式 |  AURORA
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

<link rel="alternate" href="/atom.xml" title="AURORA" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-正则表达式"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  正则表达式
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/02/02/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" class="article-date">
  <time datetime="2020-02-01T16:00:00.000Z" itemprop="datePublished">2020-02-02 00:02:00</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a> / <a class="article-category-link" href="/categories/%E6%8A%80%E6%9C%AF/%E5%89%8D%E7%AB%AF/">前端</a> / <a class="article-category-link" href="/categories/%E6%8A%80%E6%9C%AF/%E5%89%8D%E7%AB%AF/JavaScript/">JavaScript</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">11 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="RegExp类型"><a href="#RegExp类型" class="headerlink" title="RegExp类型"></a>RegExp类型</h1><p>文章转载自<a href="https://www.cnblogs.com/lnlvinso/p/10467941.html" target="_blank" rel="noopener">js中正则表达式的使用</a>，有部分删改.<br><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvNjIzNDYxLzIwMTkwMy82MjM0NjEtMjAxOTAzMDMyMTU3MTQ3OTItMzQxNjM1NjM3LnBuZw?x-oss-process=image/format,png" alt="在这里插入图片描述"></p>
<a id="more"></a>
<p>JS中使用正则表达式有两种方式</p>
<h3 id="1、正则表达式字面量"><a href="#1、正则表达式字面量" class="headerlink" title="1、正则表达式字面量"></a>1、正则表达式字面量</h3><p> var expression = / <em>pattern</em>  /  <em>flags</em><br> 其中的pattern部分可以使任何简单或复杂的正则表达式。每个正则表达式可以包含一个或多个flags，用以标注正则表达式的匹配模式</p>
<ul>
<li>g：表示全局模式，应用于所有匹配的字符串</li>
<li>i：表示不区分大小写，即在匹配过程中忽略字符串的大小写</li>
<li>m：表示多行匹配</li>
</ul>
<h3 id="2、使用RegExp构造函数"><a href="#2、使用RegExp构造函数" class="headerlink" title="2、使用RegExp构造函数"></a>2、使用RegExp构造函数</h3><p>var reg = new RegExp(“正则表达式”, “匹配模式”);</p>
<h3 id="两者的区别"><a href="#两者的区别" class="headerlink" title="两者的区别"></a>两者的区别</h3><ol>
<li>一些字符的转义<br> 使用RegExp构造函数时，所有元字符必须进行双重转义，转义过的字符也是如此<br> 例如：\通常被转义成为\，但在正则表达式字符串中会变成\\<ol start="2">
<li>正则表达式字面量和使用RegExp构造函数创建的正则表达式不一样（此问题只存在ES3，ES5已经做出修改）<br>字面量方法始终会共享同一个RegExp实例，而使用构造函数创建的每一个RegExp实例都是一个新实例<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="literal">null</span></span><br><span class="line"><span class="keyword">var</span> i</span><br><span class="line"><span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++)&#123;</span><br><span class="line">	re = <span class="regexp">/cat/g</span></span><br><span class="line">	re.test(<span class="string">"catastrophe"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//匹配成功一次之后匹配失败</span></span><br><span class="line"><span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++)&#123;</span><br><span class="line">	re = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"cat"</span>, <span class="string">'g'</span>)</span><br><span class="line">	re.test(<span class="string">"catastrophe"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//十次都匹配成功</span></span><br></pre></td></tr></table></figure>
在第一个循环中，实际上只为/cat/指定了一个RegExp实例，又因为实例属性不会重置，所以当第二次及之后调用test()方法时，就会从上一次匹配的末尾继续查找匹配，并不会从头匹配，因此匹配失败。<br>第二个循环则每次都是一个新的RegExp实例，每次匹配都从字符串开头匹配，所以可匹配成功</li>
</ol>
</li>
</ol>
<h2 id="正则的一些元字符"><a href="#正则的一些元字符" class="headerlink" title="正则的一些元字符"></a>正则的一些元字符</h2><p>一些特殊含义的元字符</p>
<blockquote>
<p>\d : 0-9之间的任意一个数字  \d只占一个位置<br>\w : 数字，字母 ，下划线 0-9 a-z A-Z _<br>\s : 空格或者空白等<br>\D : 除了\d<br>\W : 除了\w<br>\S : 除了\s<br> . : 除了\n之外的任意一个字符<br> \ : 转义字符<br> | : 或者<br>() : 分组<br>\n : 匹配换行符<br>\b : 匹配边界 字符串的开头和结尾 空格的两边都是边界 =&gt; 不占用字符串位数<br> ^ : 限定开始位置 =&gt; 本身不占位置<br> $ : 限定结束位置 =&gt; 本身不占位置<br>[a-z] : 任意字母 []中的表示任意一个都可以<br>[^a-z] : 非字母 []中^代表除了<br>[abc] : abc三个字母中的任何一个 [^abc]除了这三个字母中的任何一个字符</p>
</blockquote>
<p>代表次数的特殊元字符</p>
<blockquote>
<p> *  : 0到多个<br> + : 1到多个<br>? : 0次或1次 可有可无<br>{n} : 正好n次；<br>{n,} : n到多次<br>{n,m} : n次到m次</p>
</blockquote>
<p>量词出现在元字符后面 如\d+，限定出现在前面的元字符的次数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'    a string   '</span></span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/^\s+|\s+$/g</span></span><br><span class="line">str.replace(reg, <span class="string">''</span>)<span class="comment">//匹配字符串开头和结尾的空格</span></span><br><span class="line">str <span class="comment">//a string</span></span><br></pre></td></tr></table></figure>
<h2 id="正则中的-和"><a href="#正则中的-和" class="headerlink" title="正则中的[]和()"></a>正则中的[]和()</h2><p>一般[]中的字符没有特殊含义，+就表示+，但是像\w这种还是会有特殊含义</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc'</span></span><br><span class="line"><span class="keyword">var</span> str1 = <span class="string">'.bc'</span></span><br><span class="line"><span class="keyword">var</span> str2 = <span class="string">'fbc'</span></span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/[.ab]bc/</span></span><br><span class="line">reg.test(str)<span class="comment">//true</span></span><br><span class="line">reg.test(str1)<span class="comment">//true</span></span><br><span class="line">reg.test(str2)<span class="comment">//false</span></span><br></pre></td></tr></table></figure>
<p>[]中不会出现两位数，举个栗子：要求匹配年龄为18-65岁之间的人</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这样写是不对的：</span></span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/[18-65]/</span></span><br><span class="line">reg.test(<span class="string">'50'</span>)</span><br><span class="line"><span class="comment">//error</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//实际上，这个问题可以拆开解决</span></span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/[18|19] | [2-5]\d | (6[0-5])/</span></span><br><span class="line">reg.test(<span class="string">'42'</span>)<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<p>()的作用：</p>
<ul>
<li>提高优先级</li>
<li>分组（重复子项）</li>
</ul>
<h3 id="什么是分组"><a href="#什么是分组" class="headerlink" title="什么是分组"></a>什么是分组</h3><p>如果需要对多个字符进行重复，此时我们就要用到分组，我们可以使用小括号（）来指定要重复的子表达式，然后对这个子表达式进行重复，例如：(abc)? 表示0个或1个abc 这里一 个括号的表达式就表示一个分组分组可以分为两种形式，捕获组和非捕获组。</p>
<h4 id="捕获组"><a href="#捕获组" class="headerlink" title="捕获组"></a>捕获组</h4><p>捕获组可以通过从左到右<strong>计算其开括号</strong>来编号 。例如，在表达式(A)(B( C )) 中，存在四个这样的组：<br><img src="https://img-blog.csdnimg.cn/20200208113045702.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTIwMzE5NTg=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/(\d&#123;2&#125;).(\d&#123;2&#125;).(\d&#123;4&#125;)/</span>;</span><br><span class="line"><span class="keyword">let</span> originString = <span class="string">'02.08.2020'</span>;</span><br><span class="line">reg.test(originString); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">1</span>; <span class="comment">//02</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">2</span>; <span class="comment">//08</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">2</span>; <span class="comment">//2020</span></span><br></pre></td></tr></table></figure><br>在上面这个例子里，我们有三组括号，形成了三个捕获组，正则表达式（在javaScript里就是我们的RegExp）会缓存捕获组所匹配的串，以$n表示，n就代表这第几个捕获组。</p>
<p>我们知道String的replace()方法经常和正则表达式一起使用。在replace()方法里，我们可以直接使用捕获组的结果：将上面的栗子中02.08.2020改为2020-02-08</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/(\d&#123;2&#125;).(\d&#123;2&#125;).(\d&#123;4&#125;)/</span>;</span><br><span class="line"><span class="keyword">let</span> originString = <span class="string">'02.08.2020'</span>;</span><br><span class="line"><span class="keyword">let</span> newString = originString.replace(reg, <span class="string">'$3-$1-$2'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(newString);<span class="comment">//"2020-02-08"</span></span><br></pre></td></tr></table></figure>
<h4 id="非捕获组"><a href="#非捕获组" class="headerlink" title="( ? : )  非捕获组"></a>( ? : )  非捕获组</h4><p>有的时候我们可能只想匹配分组，但是并不想缓存（不想捕获）匹配到的结果，就可以在我们的分组模式前面加上?:。例如上面的时间的例子，我们不想捕获第一个分组的结果，就可以这么做：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/(?:\d&#123;2&#125;).(\d&#123;2&#125;).(\d&#123;4&#125;)/</span>;</span><br><span class="line"><span class="keyword">let</span> originString = <span class="string">'02.08.2020'</span>;</span><br><span class="line">reg.test(originString); <span class="comment">//true</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">1</span>; <span class="comment">//02</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">2</span>; <span class="comment">//2020</span></span><br><span class="line">originString.match(reg);<span class="comment">// ["10.25.2017", "25", "2017", index: 0, input: "10.25.2017", groups: undefined</span></span><br></pre></td></tr></table></figure>
<p>此时RegExp.$1不是数字02，而是08</p>
<p><strong>注意：组0始终代表整个表达式</strong></p>
<h3 id="positive-lookahead-正向前瞻型捕获"><a href="#positive-lookahead-正向前瞻型捕获" class="headerlink" title="(?=) positive lookahead 正向前瞻型捕获"></a>(?=) positive lookahead 正向前瞻型捕获</h3><p>现在有一个字符串：</p>
<blockquote>
<p>1 apple costs 10$</p>
</blockquote>
<p>，现需要捕获美元符号之前的价格，但不能匹配到句子开头的1，这个时候就需要用到正向前瞻性捕获</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d+(?=$)/g</span>;</span><br><span class="line"><span class="keyword">let</span> reg1 = <span class="regexp">/\d+/g</span>;</span><br><span class="line"><span class="keyword">let</span> str = <span class="string">'1 apple costs 10$'</span>;</span><br><span class="line">str.match(reg); <span class="comment">//["10"]</span></span><br><span class="line">str.match(reg1); <span class="comment">//["1", "10"]</span></span><br></pre></td></tr></table></figure>
<p>通俗来说，正向前瞻性匹配x( ? = y)就是匹配x，要求是x后面必须跟着y</p>
<h3 id="negative-lookahead-负向前瞻型捕获"><a href="#negative-lookahead-负向前瞻型捕获" class="headerlink" title="(?!) negative lookahead 负向前瞻型捕获"></a>(?!) negative lookahead 负向前瞻型捕获</h3><p>了解了正向前瞻性捕获，负向前瞻性捕获也就很好理解，/x(?!y)/ 匹配x, 但是必须当<strong>x的后面不是</strong>y的情况下。直接上个例子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d+(?!€)/g</span>;</span><br><span class="line"><span class="keyword">let</span> str = <span class="string">'1 apple costs 2€'</span>;</span><br><span class="line">str.match(reg); [<span class="string">'1'</span>]</span><br></pre></td></tr></table></figure>
<h3 id="lt-positive-lookbehind-正向后顾型捕获"><a href="#lt-positive-lookbehind-正向后顾型捕获" class="headerlink" title="(?&lt;=) positive lookbehind 正向后顾型捕获"></a>(?&lt;=) positive lookbehind 正向后顾型捕获</h3><p>后顾型和前瞻型正好相反，意思就是：<br>/(?&lt;=y)x/ 匹配x, 但是只在【前面】【有】y的情况下<br>来看一个例子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"1 turkey costs $2"</span>;</span><br><span class="line"><span class="built_in">console</span>.log( str.match(<span class="regexp">/(?&lt;=\$)\d+/g</span>) ); <span class="comment">//["2"]</span></span><br></pre></td></tr></table></figure>
<h3 id="lt-negative-lookbehind-负向后顾型捕获"><a href="#lt-negative-lookbehind-负向后顾型捕获" class="headerlink" title="(?&lt;!) negative lookbehind 负向后顾型捕获"></a>(?&lt;!) negative lookbehind 负向后顾型捕获</h3><p>负向就是与正向相反，那么负向后顾型捕获就是：<br>/(?&lt;=y)x/ 匹配x, 但是只在【前面】【没有】y的情况下<br>来看一个例子：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">"1 turkey costs $2"</span>;</span><br><span class="line"><span class="built_in">console</span>.log( str.match(<span class="regexp">/(?&lt;!\$)\d+/g</span>) ); <span class="comment">//['1']</span></span><br></pre></td></tr></table></figure></p>
<h3 id="一些其他情况"><a href="#一些其他情况" class="headerlink" title="一些其他情况"></a>一些其他情况</h3><p>默认情况下上面的前瞻后顾4种都是默认不匹配捕获组里面的内容的，也就是不匹配括号里的条件的。例如我们的正向前瞻/d+(?=€)/g，只会匹配到数字，并不会匹配到€。如果我们想要也匹配到€怎么办呢？答案就是给€也包上一个括号：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//前瞻型</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">"1 turkey costs 2€"</span>;</span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/\d+(?=(€))/</span>; </span><br><span class="line">str.match(reg); <span class="comment">//["2", "€", index: 15, input: "1 turkey costs 2€", groups: undefined]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//后顾型</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">"1 turkey costs $2"</span>;</span><br><span class="line"><span class="keyword">let</span> reg = <span class="regexp">/(?&lt;=(\$|£))\d+/</span>;</span><br><span class="line"><span class="built_in">console</span>.log( str.match(reg) ); <span class="comment">//["2", "$", index: 16, input: "1 turkey costs $2", groups: undefined]</span></span><br></pre></td></tr></table></figure>
<h2 id="正则的一些特性"><a href="#正则的一些特性" class="headerlink" title="正则的一些特性"></a>正则的一些特性</h2><ul>
<li>贪婪性</li>
</ul>
<blockquote>
<p>所谓的贪婪性就是正则在捕获时，每一次会尽可能多的去捕获符合条件的内容。<br>如果我们想尽可能的少的去捕获符合条件的字符串的话，可以在量词元字符后加?</p>
</blockquote>
<ul>
<li>懒惰型</li>
</ul>
<blockquote>
<p>懒惰性则是正则在成功捕获一次后不管后边的字符串有没有符合条件的都不再捕获。<br>如果想捕获目标中所有符合条件的字符串的话，我们可以用标识符g来标明是全局捕获</p>
</blockquote>
<h2 id="一些正则常用的方法"><a href="#一些正则常用的方法" class="headerlink" title="一些正则常用的方法"></a>一些正则常用的方法</h2><h3 id="reg-test"><a href="#reg-test" class="headerlink" title="reg.test()"></a>reg.test()</h3><p>用来验证字符串是否符合正则 符合返回true 否则返回false</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc'</span>;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/\w+/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.test(str));  <span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<h3 id="reg-exec-用来捕获符合规则的字符串"><a href="#reg-exec-用来捕获符合规则的字符串" class="headerlink" title="reg.exec() 用来捕获符合规则的字符串"></a>reg.exec() 用来捕获符合规则的字符串</h3><p>当我们用exec进行捕获时，如果正则没有加’g’标识符，则exec捕获的每次都是同一个，当正则中有’g’标识符时 捕获的结果就不一样了<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//没有g</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">'abc123cba456aaa789'</span>;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/\d+/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str))</span><br><span class="line"><span class="comment">//  ["123", index: 3, input: "abc123cba456aaa789"];</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.lastIndex)</span><br><span class="line"><span class="comment">// lastIndex : 0 </span></span><br><span class="line"></span><br><span class="line">reg.exec捕获的数组中 </span><br><span class="line"><span class="comment">// [0:"123",index:3,input:"abc123cba456aaa789"]</span></span><br><span class="line"><span class="number">0</span>:<span class="string">"123"</span> 表示我们捕获到的字符串</span><br><span class="line">index:<span class="number">3</span> 表示捕获开始位置的索引</span><br><span class="line">input 表示原有的字符串</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//有g</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">'abc123cba456aaa789'</span>;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/\d+/g</span>;  <span class="comment">//此时加了标识符g</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.lastIndex)</span><br><span class="line"><span class="comment">// lastIndex : 0 </span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str))</span><br><span class="line"><span class="comment">//  ["123", index: 3, input: "abc123cba456aaa789"]</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.lastIndex)</span><br><span class="line"><span class="comment">// lastIndex : 6</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str))</span><br><span class="line"><span class="comment">// ["456", index: 9, input: "abc123cba456aaa789"]</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.lastIndex)</span><br><span class="line"><span class="comment">// lastIndex : 12</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str))</span><br><span class="line"><span class="comment">// ["789", index: 15, input: "abc123cba456aaa789"]</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.lastIndex)</span><br><span class="line"><span class="comment">// lastIndex : 18</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str))</span><br><span class="line"><span class="comment">// null</span></span><br><span class="line"><span class="built_in">console</span>.log(reg.lastIndex)</span><br><span class="line"><span class="comment">// lastIndex : 0</span></span><br></pre></td></tr></table></figure><br>每次调用exec方法时,捕获到的字符串都不相同<br>lastIndex ：这个属性记录的就是下一次捕获从哪个索引开始。<br>当未开始捕获时，这个值为0。<br>如果当前次捕获结果为null。那么lastIndex的值会被修改为0.下次从头开始捕获。<br>而且这个lastIndex属性还支持人为赋值。</p>
<p>exec还受分组的影响</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'2017-01-05'</span>;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/-(\d+)/g</span></span><br><span class="line"><span class="comment">// ["-01", "01", index: 4, input: "2017-01-05"]</span></span><br><span class="line"><span class="string">"-01"</span> : 正则捕获到的内容</span><br><span class="line"><span class="string">"01"</span>  : 捕获到的字符串中的小分组中的内容</span><br></pre></td></tr></table></figure>
<h3 id="str-match-reg-如果匹配成功，就返回匹配成功的数组，如果匹配不成功，就返回null"><a href="#str-match-reg-如果匹配成功，就返回匹配成功的数组，如果匹配不成功，就返回null" class="headerlink" title="str.match(reg) 如果匹配成功，就返回匹配成功的数组，如果匹配不成功，就返回null"></a>str.match(reg) 如果匹配成功，就返回匹配成功的数组，如果匹配不成功，就返回null</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc123cba456aaa789'</span>;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/\d+/</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str));</span><br><span class="line"><span class="comment">//["123", index: 3, input: "abc123cba456aaa789"]</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg));</span><br><span class="line"><span class="comment">//["123", index: 3, input: "abc123cba456aaa789"]</span></span><br></pre></td></tr></table></figure>
<p>可以看到，两者的结果是一样的，因为我们没有设置全局匹配，当加了g之后：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc123cba456aaa789'</span>;</span><br><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/\d+/g</span>;</span><br><span class="line"><span class="built_in">console</span>.log(reg.exec(str));</span><br><span class="line"><span class="comment">// ["123", index: 3, input: "abc123cba456aaa789"]</span></span><br><span class="line"><span class="built_in">console</span>.log(str.match(reg));</span><br><span class="line"><span class="comment">// ["123", "456", "789"]</span></span><br></pre></td></tr></table></figure>
<h3 id="str-replace-——-正则的好伙伴"><a href="#str-replace-——-正则的好伙伴" class="headerlink" title="str.replace() —— 正则的好伙伴"></a>str.replace() —— 正则的好伙伴</h3><p>正则去匹配字符串，匹配成功的字符去替换成新的字符串<br>写法：str.replace(reg,newStr);</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'a111bc222de'</span>;</span><br><span class="line"><span class="keyword">var</span> res = str.replace(<span class="regexp">/\d/g</span>,<span class="string">'Q'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(res)</span><br><span class="line"><span class="comment">// "aQQQbcQQQde"</span></span><br></pre></td></tr></table></figure>
<p>replace的第二个参数也可以是一个函数<br>str.replace(reg,fn);<br>这个函数会接收三个参数：模式的匹配项，模式匹配项在字符串中的位置，原始字符串</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'2017-01-06'</span>;</span><br><span class="line">str = str.replace(<span class="regexp">/-\d+/g</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">arguments</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">/*["-01", 4, "2017-01-06"]</span></span><br><span class="line"><span class="comment">["-06", 7, "2017-01-06"]</span></span><br><span class="line"><span class="comment">"2017undefinedundefined"*/</span></span><br></pre></td></tr></table></figure>
<p>如果我们需要替换replace中正则找到的字符串，函数中需要一个返回值去替换正则捕获的内容<br>再来个简单的栗子：捕获url中的参数值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">pro</span>)</span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">queryString</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> obj = &#123;&#125;,</span><br><span class="line">            reg = <span class="regexp">/([^?&amp;#+]+)=([^?&amp;#+]+)/g</span>;</span><br><span class="line">        <span class="keyword">this</span>.replace(reg,<span class="function"><span class="keyword">function</span>(<span class="params">$<span class="number">0</span>,$<span class="number">1</span>,$<span class="number">2</span></span>)</span>&#123;</span><br><span class="line">            obj[$<span class="number">1</span>] = $<span class="number">2</span>;</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> obj;</span><br><span class="line">    &#125;</span><br><span class="line">    pro.queryString = queryString;</span><br><span class="line">&#125;(<span class="built_in">String</span>.prototype));</span><br></pre></td></tr></table></figure>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/02/04/react-hook%E4%B8%AD%E6%95%B0%E7%BB%84state%E7%9A%84%E6%9B%B4%E6%96%B0/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            react-hook中数组state的更新
          
        </div>
      </a>
    
    
      <a href="/2020/01/31/%E5%8F%98%E9%87%8F/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">变量</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "2eBxdPB3eEwraN7IpwFckzeL-gzGzoHsz",
    app_key: "VGgCefQEluVxHdyMsm1Dpeih",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "评论一下我的文章吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> AURORA_ZXH
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        Powered by <a href="https://hexo.io" target="_blank">Hexo</a>
        <span class="division">|</span>
        Theme - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.jpg" alt="AURORA"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="http://aurora-zxh.lofter.com/" target="_blank" rel="noopener">随记</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/link">友链</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.png">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wxpay.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>